package com.my.getway.white;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class WhitePageListConfig {


    @Value("${ms-api-whitelist.view-board}")
    private String msViewBoardWhiteList;


    @Value("${ms-api-whitelist.wangge-exam}")
    private String msWanggeExamWhiteList;

    private static Map<String, Set<String>> DATA = new HashMap<>();


    /**
     * 看板白名单
     */
    private static Set<String> VIEW_BOARD_WHITELIST = new HashSet<>();

    /**
     * 考试模块白名单
     */
    private static Set<String> EXAM_WHITELIST = new HashSet<>();


   public  boolean isAtWhitePageList(String url){
       //这里需要细化到 服务下的api
       boolean f1 =  isAtViewBoardWhiteList(url);
       boolean f2 =  isAtWanggeExamWhiteList(url);
       return f1 || f2;

    }
    public  void init(){
        initWanggeExamWhiteList();
        initViewBoardWhiteList();
        DATA.put(ServiceNameConstants.WANGGE_EXAM_SERVICE,EXAM_WHITELIST);
        DATA.put(ServiceNameConstants.VIEW_BOARD_SERVICE,VIEW_BOARD_WHITELIST);

    }
    private void  initViewBoardWhiteList(){
        if (StringUtils.isNotEmpty(msViewBoardWhiteList)){
            if (VIEW_BOARD_WHITELIST.size() == 0){
                String uris [] = msViewBoardWhiteList.split(",");
                for (String e : uris
                ) {
                    VIEW_BOARD_WHITELIST.add(e);
                }
            }
        }
    }

    private void  initWanggeExamWhiteList(){
        if (StringUtils.isNotEmpty(msWanggeExamWhiteList)){
            if (EXAM_WHITELIST.size() == 0){
                String uris [] = msWanggeExamWhiteList.split(",");
                for (String e : uris
                ) {
                    EXAM_WHITELIST.add(e);
                }
            }
        }
    }

    private boolean isAtViewBoardWhiteList(String uri){
        boolean f = false;
        Iterator<String> iterator =  VIEW_BOARD_WHITELIST.iterator();
        while (iterator.hasNext()){
            String s = iterator.next();
            if (uri.contains(s)){
                f = true;
                break;
            }
        }
        return f;

    }
    private boolean isAtWanggeExamWhiteList(String uri){
        boolean f = false;
        Iterator<String> iterator =  EXAM_WHITELIST.iterator();
        while (iterator.hasNext()){
            String s = iterator.next();
            if (uri.contains(s)){
                f = true;
                break;
            }
        }
        return f;

    }

}
